import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	//data
	state: {
		userinfo: {

		},
		needAuth: true,
		isLogin: false
	},
	//computed
	getters: {
		getUserInfo(state) {
			return state.userinfo
		},
		getNeedAuth(state) {
			return state.needAuth
		},
		getIsLogin(state) {
			return state.isLogin
		}
	},
	//method
	mutations: {
		setUserInfo(state, userinfo) {
			state.userinfo = userinfo
		},
		setNeedAuth(state, needAuth) {
			state.needAuth = needAuth
		},
		setIsLogin(state, isLogin) {
			state.isLogin = isLogin
		}
	},
	//异步的方法
	actions: {
		login(context) {
			return new Promise((resolve, reject) => {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						console.log('获取微信Code',res)
						Vue.prototype.$u.api.login({
							data: {
								code: res.code,
								token: uni.getStorageSync('token')
							}
						}).then((res) => {
							console.log(res)
							uni.setStorageSync('token', res.data.token)
							if (res.msg == '登录成功') {
								context.commit('setUserInfo', res.data.user)
								context.commit('setNeedAuth', false)
								context.commit('setIsLogin', true)
							}
							resolve('login执行完毕')
						})
					},
					fail: res => {
						Vue.prototype.$u.toast('获取code失败')
						reject('获取code失败')
					}
				})
			})
		},
		authUserInfo(context) {
			return new Promise((resolve, reject) => {
				uni.getUserInfo({
					// des: '1111',
					provider: 'weixin',
					lang: 'zh_CN',
					success: res => {
						console.log("用户", JSON.stringify(res))
						context.commit('setUserInfo', {
							nickname: res.userInfo.nickName,
							sex: res.userInfo.gender === 1,
							avatar: res.userInfo.avatarUrl,
							city: res.userInfo.city
						})
						Vue.prototype.$u.api.auth(context.state.userinfo).then(res => {
							console.log('res', res)
							if(res.msg=='授权成功'){
								context.commit('setNeedAuth',false)
							}else{
								Vue.prototype.$u.toast('授权失败')
							}
							resolve('授权执行完毕')
						})
					},
					fail:res=>{
						reject('获取信息出错')
					}
					
				})
			})
		}

	}
})

export default store
